package com.bjx.auth.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bjx.api.system.domain.vo.UserVO;
import com.bjx.api.system.service.RemoteUserService;
import com.bjx.auth.domain.vo.CodeVO;
import com.bjx.auth.domain.vo.LoginVO;
import com.bjx.auth.service.LoginService;
import com.bjx.common.core.constant.FrameConst;
import com.bjx.common.core.constant.RedisConst;
import com.bjx.common.core.domain.R;
import com.bjx.common.core.exception.BJXException;
import com.bjx.common.core.util.ImageUtil;
import com.bjx.common.redis.service.RedisService;
import com.bjx.common.security.domain.LoginUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @desc: 登录逻辑控制
 * @author: liushuai
 * @date: 2022/7/2 15:18
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Value("${encryption.private}")
    private String privateKey;

    @Resource
    private RedisService redisService;

    @Override
    public LoginUserVO login(LoginVO loginVO) {
        //根据登录用户名获取用户信息
        R<UserVO> userVOR = remoteUserService.selectUserInfoByUsername(loginVO.getUsername());
        if(userVOR.getCode() == FrameConst.FAIL){
           throw new BJXException(userVOR.getMsg());
        }
        UserVO user = userVOR.getData();
        if(ObjectUtil.isNull(user)){
            throw new BJXException("用户名或密码错误");
        }
        String password = SaSecureUtil.rsaDecryptByPrivate(privateKey, user.getPassword());
        if(!password.equals(loginVO.getPassword())){
            throw new BJXException("用户名或密码错误");
        }
        String code = redisService.getCacheStr(RedisConst.VERIFICATION_CODE + loginVO.getUuid());
        if(StrUtil.isBlank(code)){
            throw new BJXException("验证码已过期");
        }else if(!code.equals(loginVO.getCode())){
            throw new BJXException("验证码不正确");
        }
        StpUtil.login(Long.valueOf(user.getUserId()));
        LoginUserVO loginUserVO = Convert.convert(LoginUserVO.class,user);
        loginUserVO.setToken(StpUtil.getTokenValue());
        StpUtil.getSession().set("loginUser", loginUserVO);
        return loginUserVO;
    }

    @Override
    public CodeVO getCode(HttpServletResponse response) {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        String code = captcha.getCode();
        String image = ImageUtil.imageToBase64(captcha.getImage());
        UUID uuid = UUID.randomUUID();
        redisService.setCacheObject(RedisConst.VERIFICATION_CODE + uuid.toString() ,code, 30L, TimeUnit.SECONDS);
        return CodeVO.builder()
                .image(image)
                .uuid(uuid.toString())
                .build();
    }



}
